from datetime import datetime
import App_setting as setting


def info(s: str = '', ip=''):
    insert_text = ''
    if ip:
        insert_text = f'[{ip}]'
    text = f'[INFO]{insert_text}[{__time__()}]' + s
    __print__(text)
    __write__(text)


def data(s: str = '', ip=''):
    insert_text = ''
    if ip:
        insert_text = f'[{ip}]'
    text = f'[DATA]{insert_text}[{__time__()}]' + s
    __print__(text)
    __write__(text)
    __write__(text, setting.path.file_datalog)


def warn(s: str = '', ip=''):
    insert_text = ''
    if ip:
        insert_text = f'[{ip}]'
    text = f'{insert_text}[{__time__()}]' + s
    __print__(f'[{yellow("WARN")}]' + text)
    __write__('[WARN]' + text)


def error(s: str = '', ip=''):
    insert_text = ''
    if ip:
        insert_text = f'[{ip}]'
    text = f'{insert_text}[{__time__()}]' + s
    __print__(f'[{red("ERROR")}]' + text)
    __write__('[ERROR]' + text)


def __print__(s: str = '') -> None:
    print(s)


def __time__():
    return datetime.now().strftime("%y%m%d %H:%M:%S")


def __write__(s: str = '', destination_file: str = setting.path.file_mylog) -> None:
    with open(destination_file, 'a') as f:
        f.write(s)
        if not s.endswith('\n'):
            f.write('\n')
        f.close()


def yellow(s: str = ''):
    return '\033[33m' + s + '\033[0m'


def red(s: str = ''):
    return '\033[31m' + s + '\033[0m'
